<?php 
if($_SERVER["PHP_SELF"] != $CONFIG['phpself'])
exit();

//CREATION DE L'ACHIEVEMENT
$args=array();
$j=0;
for ($i=6;$i>0;$i--)
{
	//echo $_SESSION['AchTypeofAchievement'][$i];
	switch($_SESSION['AchTypeofAchievement'][$i])
	{
		case "kill": 
			$table_insert[$j]="achmw_blocks_kill";
			if(	$_SESSION['AchWhoWinKill'][$i] == "suicide" || $_SESSION['AchWhoWinKill'][$i] == "victim" )
				$args[$j][1] = 0;
			elseif ( $_SESSION['AchWhoWinKill'][$i] == "attacker" )
				$args[$j][1] = 1;
			else
				$args[$j][1] = 2;
			$args[$j][2] = $_SESSION['AchKillAttackSteamIDText'][$i];
			$args[$j][3] = $_SESSION['AchKillVictimSteamIDText'][$i];
			$args[$j][4] = $_SESSION['AchKillAssistSteamIDText'][$i];
			$args[$j][5] = $_SESSION['AchKillWeapon'][$i];
			$args[$j][6] = $_SESSION['AchKillMap'][$i];	
			$args[$j][7] = $_SESSION['AchKillAttackclass'][$i];
			$args[$j][8] = $_SESSION['AchKillVictimclass'][$i];
			$args[$j][9] = $_SESSION['AchKillAssistclass'][$i];
			if(	$_SESSION['AchWhoWinKill'][$i] == "suicide" )
				$args[$j][10] = 1;
			else
				$args[$j][10] = 0;
			$args[$j][11] = $_SESSION['AchKillCritz'][$i];
			$args[$j][12] = $_SESSION['AchKillDomination'][$i];
			$args[$j][13] = $_SESSION['AchKillRevenge'][$i];
			$args[$j][14] = $_SESSION['AchKillHeadShot'][$i];
			$args[$j][15] = $_SESSION['AchKillBackstab'][$i];
			$args[$j][16] = $_SESSION['AchKillDeadRinger'][$i];
			
			if($_SESSION['AchEngineerObjectName1'][$i] == "sentry1")
				$args[$j][17] = 3;
			elseif($_SESSION['AchEngineerObjectName1'][$i] == "dispenser1")
				$args[$j][17] = 0;
			elseif($_SESSION['AchEngineerObjectName1'][$i] == "teleentry1")
				$args[$j][17] = 1;	
			elseif($_SESSION['AchEngineerObjectName1'][$i] == "teleexit1")
				$args[$j][17] = 2;	
			else
				$args[$j][17] = -1;
			
			if($_SESSION['AchEngineerDestroyBuild1'][$i] == "yes")
				$args[$j][18] = 1;
			elseif ($_SESSION['AchEngineerDestroyBuild1'][$i] == "no")
				$args[$j][18] = 0;
			else
				$args[$j][18] = -1;
				
			$args[$j][0] = $_SESSION['AchKillAmount'][$i]; 
		
		break;
		case "hurt": 
			$table_insert[$j]="achmw_blocks_hurt";
			if(	$_SESSION['AchWhoWinHurt'][$i] == "victim" )
				$args[$j][1] = 0;
			elseif ( $_SESSION['AchWhoWinHurt'][$i] == "attacker" )
				$args[$j][1] = 1;
			else
				$args[$j][1] = -1;
			$args[$j][2] = $_SESSION['AchHurtAttackerSteamIDText'][$i];
			$args[$j][3] = $_SESSION['AchHurtVictimSteamIDText'][$i];
			$args[$j][4] = $_SESSION['AchHurtCritz'][$i];
			if($_SESSION['AchHurtDmg_min'][$i] == "")
				$args[$j][5] = -1;
			else
				$args[$j][5] = $_SESSION['AchHurtDmg_min'][$i];
				
			if($_SESSION['AchHurtDmg_max'][$i] == "")
				$args[$j][6] = -1;
			else
				$args[$j][6] = $_SESSION['AchHurtDmg_max'][$i];
			
			$args[$j][7] = $_SESSION['AchHurtSelfdmg'][$i];
			$args[$j][8] = $_SESSION['AchHurtMap'][$i];
			$args[$j][9] = $_SESSION['AchHurtAttacker_class'][$i];
			$args[$j][10] = $_SESSION['AchHurtVictim_class'][$i];
			$args[$j][0] = $_SESSION['AchHurtAmount'][$i];
		break;
		case "engineer": 
			if($_SESSION['AchEngineerChoose'][$i] == "build")
				$table_insert[$j]="achmw_blocks_engineer_pda";
			elseif ($_SESSION['AchEngineerChoose'][$i] == "teleport")
				$table_insert[$j]="achmw_blocks_engineer_teleport";
			
			if($_SESSION['AchEngineerChoose'][$i] == "build")
			{
				if($_SESSION['AchEngineerObjectName'][$i] == "sentry")
					$args[$j][1] = 3;
				elseif($_SESSION['AchEngineerObjectName'][$i] == "dispenser")
					$args[$j][1] = 0;
				elseif($_SESSION['AchEngineerObjectName'][$i] == "teleentry")
					$args[$j][1] = 1;	
				elseif($_SESSION['AchEngineerObjectName'][$i] == "teleexit")
					$args[$j][1] = 2;
				else
					$args[$j][1] = -1;
			}
			elseif ($_SESSION['AchEngineerChoose'][$i] == "teleport")
			{
				if($_SESSION['AchEngineerDestroy'][$i] == "victim")
					$args[$j][1] = 0;
				elseif ($_SESSION['AchEngineerDestroy'][$i] == "attacker")
					$args[$j][1] = 1;
				else
					$args[$j][1] = -1;
				$args[$j][2] = 	$_SESSION['AchEngineerOwnerSteamID'][$i];
				$args[$j][3] = 	$_SESSION['AchEngineerUserSteamID'][$i];
				$args[$j][4] = 	$_SESSION['AchEngineerUserClass'][$i];
				if ($_SESSION['AchEngineerSelfUse'][$i] == "yes")
				{
					$args[$j][5] = 1;
				}
				else
				{
					$args[$j][5] = 0;
				}
				$args[$j][0] = $_SESSION['AchEngineerAmount'][$i]; ;
			}
			
		break;
		case "heavy": 
			if($_SESSION['AchHeavySandwitch'][$i] == "own")
				$table_insert[$j]="achmw_blocks_eatsandwich";
			else
				$table_insert[$j]="achmw_blocks_stealsandwich";
			
				if($_SESSION['AchHeavySandwitchSteal'][$i] == "victim")
					$args[$j][1] = 0;
				elseif ($_SESSION['AchHeavySandwitchSteal'][$i] == "attacker")
					$args[$j][1] = 1;
				else
					$args[$j][1] = -1;

		break;
		case "sniper": 
			if($_SESSION['AchHeavySandwitch'][$i] == "jarate")
				$table_insert[$j]="achmw_blocks_jarate";
			elseif ($_SESSION['AchHeavySandwitch'][$i] == "razor")
				$table_insert[$j]="achmw_blocks_razorback";
				
			if( $_SESSION['AchSniperJarateRazor'][$i] == "victim" ) 
				$args[$j][1] = 0;
			elseif ($_SESSION['AchSniperJarateRazor'][$i] == "attacker")
				$args[$j][1] = 1;
			else
				$args[$j][1] = -1;
			$args[$j][2] = $_SESSION['AchSniperVictimSteamID'][$i];
			$args[$j][0] = $_SESSION['AchSniperAmount'][$i]; 
		break;
		case "medic": 
			$table_insert[$j]="achmw_blocks_ubercharge";
			if( $_SESSION['AchMedicWho'][$i] == "medic" ) 
				$args[$j][1] = 0;
			elseif ($_SESSION['AchMedicWho'][$i] == "assist")
				$args[$j][1] = 1;
			$args[$j][2] = $_SESSION['AchMedicUber'][$i]; 
			$args[$j][3] = $_SESSION['AchMedicAssistSteamID'][$i];
			$args[$j][0] = $_SESSION['AchMedicAmount'][$i]; 
		break;
		case "scout": 
			if($_SESSION['AchScoutAchievements'][$i] == "stunt")
			{
				$table_insert[$j]="achmw_blocks_stunner";
				$args[$j][1] = $_SESSION['AchScoutAchievementsStunt'][$i];
			}
			elseif($_SESSION['AchScoutAchievements'][$i] == "bonk")
				$table_insert[$j]="achmw_blocks_bonk"; 
			$args[$j][0] = $_SESSION['AchScoutAmount'][$i]; 
		break;
		case "map": 
			$table_insert[$j]="achmw_blocks_capture";
			if( $_SESSION['AchWhoWinTeamMap'][$i] == "blu" ) 
				$args[$j][1] = 1;
			elseif ($_SESSION['AchWhoWinTeamMap'][$i] == "red")
				$args[$j][1] = 2;
			else
				$args[$j][1] = -1;
			
			$args[$j][2] = $_SESSION['AchMapName'][$i]; 
			$args[$j][3] = $_SESSION['AchMapCaperSteamIDText'][$i]; 
			$args[$j][4] = @empty($_SESSION['AchMapNumberOfPoint'][$i]) ? -1 : $_SESSION['AchMapNumberOfPoint'][$i];
			$args[$j][5] = @empty($_SESSION['AchMapMinCaper'][$i]) ? -1 : $_SESSION['AchMapMinCaper'][$i]; 
			$args[$j][6] = @empty($_SESSION['AchMapMaxCaper'][$i])  ? -1 : $_SESSION['AchMapMaxCaper'][$i]; 
			$args[$j][7] = $_SESSION['AchMapCapturerClass'][$i]; 
			$args[$j][0] = $_SESSION['AchMapAmount'][$i]; 
		break;
		case "fun": 
		if ($_SESSION['AchFunRadio'][$i]=="2")
			$table_insert[$j]="achmw_blocks_callmedic";
		else
		{
			$table_insert[$j]="achmw_blocks_say";
			$args[$j][1]=$_SESSION['AchFunRadioExact'][$i]; 
			$args[$j][2]=$_SESSION['AchFunSayValue'][$i]; 
			$args[$j][3]=$_SESSION['AchMapFun'][$i]; 
		}
		$args[$j][0] = $_SESSION['AchFunAmount'][$i]; 
		break;
		case "spy": 
			$table_insert[$j]="achmw_blocks_sapped";
			if( $_SESSION['AchSpyWho'][$i] == "engineer" ) 
				$args[$j][1] = 0;
			elseif ($_SESSION['AchSpyWho'][$i] == "spy")
				$args[$j][1] = 1;
			if($_SESSION['AchSpyObjectName'][$i] == "sentry")
					$args[$j][2] = 3;
				elseif($_SESSION['AchSpyObjectName'][$i] == "dispenser")
					$args[$j][2] = 0;
				elseif($_SESSION['AchSpyObjectName'][$i] == "teleentry")
					$args[$j][2] = 1;	
				elseif($_SESSION['AchSpyObjectName'][$i] == "teleexit")
					$args[$j][2] = 2;
				else
					$args[$j][2] = -1;
			$args[$j][3] = $_SESSION['AchSpyVictimSteamID'][$i];
			$args[$j][0] = $_SESSION['AchSpyAmount'][$i]; 
		break;
		case "soldier": 
			if($_SESSION['AchSoldierAction'][$i] == "jump")
				$table_insert[$j]="achmw_blocks_rocketjump";
			elseif ($_SESSION['AchSoldierAction'][$i] == "buff")
				$table_insert[$j]="achmw_blocks_banner";
			$args[$j][0] = $_SESSION['AchSoldierAmount'][$i]; 
		break;
		case "demoman": 
				$table_insert[$j]="achmw_blocks_stickyjump";
			$args[$j][0] = $_SESSION['AchDemomanAmount'][$i]; 
		break;
		case "condition": 
		$table_insert[$j]="achmw_blocks_condition";
		$args[$j][1]=$_SESSION['AchB2Conditon'][$i]; 
		if( $_SESSION['AchConditonWho'][$i] == "victim" ) 
			$args[$j][2] = 0;
		elseif ($_SESSION['AchConditonWho'][$i] == "attacker")
			$args[$j][2] = 1;
		elseif ($_SESSION['AchConditonWho'][$i] == "assist")
			$args[$j][2] = 2;
		else
			$args[$j][2] = -1;
		if( $_SESSION['AchB2Conditon'][$i] == "1" )
		{
			$args[$j][3] = $_SESSION['AchB2ConditonHealth1'][$i];
			$args[$j][4] = $_SESSION['AchB2ConditonHealth2'][$i];
			$args[$j][5] = 0;
		}
		elseif ( $_SESSION['AchB2Conditon'][$i] == "2" )
		{
			$args[$j][3] = $_SESSION['AchB2ConditonChapeau'][$i];
			$args[$j][4] = 0;
			$args[$j][5] = 0;
		}
		else
		{
			$args[$j][3] = 0;
			$args[$j][4] = 0;
			$args[$j][5] = 0;
		}
		break;
		default : $table_insert[$j]="achmw_blocks_none";
	}
	$j++;
}
$countQuery = 0;
$FAIL = false;
$messErr = "";
$j = 5;
$doNot = false;
for ($i=1;$i<6;$i++)
{
	$doNot = false;
	if(!$FAIL)
	{
		$modif=false;
		if(substr($table_insert[$j],strlen("achmw_blocks_")) == $_SESSION['nameNum'][$i])
		{
			$modif=true;
		}
		//echo substr($table_insert[$j],strlen("achmw_blocks_"))."<br/>";
		//echo $_SESSION['nameNum'][$i]."<br/>";
		//echo $table_insert[$j]."<br/>";
		if($table_insert[$j] != "achmw_blocks_none")
		{
			if ($modif)
				$sql = "UPDATE `".$table_insert[$j]."` SET ";
			else
				$sql = "INSERT INTO `".$table_insert[$j]."` VALUES (NULL";
			switch ($table_insert[$j]) 
			{
			case "achmw_blocks_kill": 
				if ($modif)
					$sql = $sql."`winner`=".$args[$j][1].",`attacker_id`='".$args[$j][2]."',`victim_id`='".$args[$j][3]."',`assist_id`='".$args[$j][4]."',`killweapon`='".$args[$j][5]."',`map`='".$args[$j][6]."',`attacker_class`='".$args[$j][7]."',`victim_class`='".$args[$j][8]."',`assister_class`='".$args[$j][9]."',`selfdmg`=".$args[$j][10].",`crit`=".$args[$j][11].",`domination`=".$args[$j][12].",`revenge`=".$args[$j][13].",`headshot`=".$args[$j][14].",`backstab`=".$args[$j][15].",`deadringer`=".$args[$j][16].",`object`=".$args[$j][17].",`was_build`=".$args[$j][18]." WHERE `id` = ".$_SESSION['AchIdTwo'][$i].";";
				else
					$sql = $sql.",".$args[$j][1].",'".$args[$j][2]."','".$args[$j][3]."','".$args[$j][4]."','".$args[$j][5]."','".$args[$j][6]."','".$args[$j][7]."','".$args[$j][8]."','".$args[$j][9]."',".$args[$j][10].",".$args[$j][11].",".$args[$j][12].",".$args[$j][13].");";
				$countQuery ++;
			break;
			case "achmw_blocks_hurt": 
				if ($modif)
					$sql = $sql."`winner`=".$args[$j][1].",`attacker_id`='".$args[$j][2]."',`victim_id`='".$args[$j][3]."',`crit`=".$args[$j][4].",`dmg_min`=".$args[$j][5].",`dmg_max`=".$args[$j][6].",`selfdmg`=".$args[$j][7].",`map`='".$args[$j][8]."',`attacker_class`='".$args[$j][9]."',`victim_class`='".$args[$j][10]."' WHERE `id` = ".$_SESSION['AchIdTwo'][$i].";";
				else
					$sql = $sql.",".$args[$j][1].",'".$args[$j][2]."','".$args[$j][3]."',".$args[$j][4].",".$args[$j][5].",".$args[$j][6].",".$args[$j][7].",'".$args[$j][8]."','".$args[$j][9]."','".$args[$j][10]."');";
				$countQuery ++;
			break;
			case "achmw_blocks_engineer_pda": 
				if ($modif)
					$sql = $sql."`object`=".$args[$j][1]." WHERE `id` = ".$_SESSION['AchIdTwo'][$i].";";
				else
					$sql = $sql.",".$args[$j][1].");";
				$countQuery ++;
			break;
			case "achmw_blocks_engineer_teleport": 
				if ($modif)
					$sql = $sql."`winner`=".$args[$j][1].",`owner_id`='".$args[$j][2]."',`user_id`='".$args[$j][3]."',`user_class`='".$args[$j][4]."',`selfuse`=".$args[$j][5]." WHERE `id` = ".$_SESSION['AchIdTwo'][$i].";";
				else
					$sql = $sql.",".$args[$j][1].",'".$args[$j][2]."','".$args[$j][3]."','".$args[$j][4]."',".$args[$j][5].");";
				$countQuery ++;
			break;
			case "achmw_blocks_eatsandwich": 
				if ($modif)
				{
					$sql = $sql.");";
					$doNot = true;
				}
				else
					$sql = $sql.");";
				$countQuery ++;
			break;
			case "achmw_blocks_stealsandwich": 
				if ($modif)
					$sql = $sql."`winner`=".$args[$j][1]." WHERE `id` = ".$_SESSION['AchIdTwo'][$i].";";
				else
					$sql = $sql.",".$args[$j][1].");";
				$countQuery ++;
			break;
			case "achmw_blocks_jarate": 
				if ($modif)
					$sql = $sql."`winner`=".$args[$j][1]." AND steamid = '".$args[$j][2]."' WHERE `id` = ".$_SESSION['AchIdTwo'][$i].";";
				else
					$sql = $sql.",".$args[$i][1].",'".$args[$i][2]."');";
				$countQuery ++;
			break;
			case "achmw_blocks_razorback": 
				if ($modif)
					$sql = $sql."`winner`=".$args[$j][1]." WHERE `id` = ".$_SESSION['AchIdTwo'][$i].";";
				else
					$sql = $sql.",".$args[$j][1].");";
				$countQuery ++;
			break;
			case "achmw_blocks_ubercharge": 
				if ($modif)
					$sql = $sql."`winner`=".$args[$j][1].",`weapons_uber`='".$args[$j][2]."' AND steamid = '".$args[$j][3]."' WHERE `id` = ".$_SESSION['AchIdTwo'][$i].";";
				else
					$sql = $sql.",".$args[$i][1].",'".$args[$i][2].",'".$args[$i][3]."');";
				$countQuery ++;
			break;
			case "achmw_blocks_stunner": 
				if ($modif)
					$sql = $sql."`winner`=".$args[$j][1]."' AND steamid = '".$args[$j][2]."' WHERE `id` = ".$_SESSION['AchIdTwo'][$i].";";
				else
					$sql = $sql.",".$args[$j][1].",'".$args[$i][2]."');";
				$countQuery ++;
			break;
			case "achmw_blocks_bonk": 
				if ($modif)
				{
					$sql = $sql.");";
					$doNot = true;
				}
				else
					$sql = $sql.");";
				$countQuery ++;
			break;
			case "achmw_blocks_rocketjump": 
				if ($modif)
				{
					$sql = $sql.");";
					$doNot = true;
				}
				else
					$sql = $sql.");";
				$countQuery ++;
			break;
			case "achmw_blocks_stickyjump": 
				if ($modif)
				{
					$sql = $sql.");";
					$doNot = true;
				}
				else
					$sql = $sql.");";
				$countQuery ++;
			break;
			case "achmw_blocks_banner": 
				if ($modif)
				{
					$sql = $sql.");";
					$doNot = true;
				}
				else
					$sql = $sql.");";
				$countQuery ++;
			break;
			case "achmw_blocks_sapped": 
				if ($modif)
					$sql = $sql."`winner`=".$args[$j][1].",`object`=".$args[$j][2].",`steamid` = '".$args[$j][3]."' WHERE `id` = ".$_SESSION['AchIdTwo'][$i].";";
				else
					$sql = $sql.",".$args[$i][1].",'".$args[$i][2]."','".$args[$i][3]."');";
				$countQuery ++;
			break;
			case "achmw_blocks_capture": 
				if ($modif)
					$sql = $sql."`winner`=0,`captureteam`=".$args[$j][1].",`map`='".$args[$j][2]."',`capturer_id` = '".$args[$j][3]."',`cp_id`=".$args[$j][4].",`min_cappers`=".$args[$j][5].",`max_cappers`=".$args[$j][6].",`capturer_class`='".$args[$j][7]."' WHERE `id` = ".$_SESSION['AchIdTwo'][$i].";";
				else
					$sql = $sql.",0,".$args[$j][1].",'".$args[$j][2]."','".$args[$j][3]."',".$args[$j][4].",".$args[$j][5].",".$args[$j][6].",'".$args[$j][7]."');";
				$countQuery ++;
			break;
			case "achmw_blocks_callmedic": 
				if ($modif)
				{
					$sql = $sql.");";
					$doNot = true;
				}
				else
					$sql = $sql.");";
				$countQuery ++;
			break;
			case "achmw_blocks_say": 
				if ($modif)
					$sql = $sql."`method`=".$args[$j][1].",`content`='".$args[$j][2]."',`map`='".$args[$j][3]."' WHERE `id` = ".$_SESSION['AchIdTwo'][$i].";";
				else
					$sql = $sql.",".$args[$j][1].",'".$args[$j][2]."','".$args[$j][3]."');";
				$countQuery ++;
			break;
			case "achmw_blocks_condition": 
				if ($modif)
					$sql = $sql."`type`=".$args[$j][1].",`target`=".$args[$j][2].",`int1`=".$args[$j][3].",`int2`=".$args[$j][4].",`int3`=".$args[$j][5]." WHERE `id` = ".$_SESSION['AchIdTwo'][$i].";";
				else
					$sql = $sql.",".$args[$j][1].",".$args[$j][2].",".$args[$j][3].",".$args[$j][4].",".$args[$j][5].");";
				$countQuery ++;
			break;
			default :
			}
			//echo $sql."<br/>";
			if($doNot == false)
			{
				$result = mysql_query($sql);
				if ($result) 
				{
					if (!$modif)
					{
						$selectMax = "DELETE FROM `".$table_insert[$j]."` WHERE id = ".$_SESSION['AchIdTwo'][$i].";";
						//echo $selectMax."<br/>";
						$resultSelectMax = mysql_query($selectMax);
						$selectMax = "SELECT MAX(id) AS id FROM `".$table_insert[$j]."`;";
						$resultSelectMax = mysql_query($selectMax);
						if ($resultSelectMax) 
						{
							$data = mysql_fetch_assoc($resultSelectMax); 
							$id_de_lachivement = intval($data['id']);
							$idOfAchievementCreat[$i] = $id_de_lachivement;
							if($idOfAchievementCreat[$i] == "")
								$idOfAchievementCreat[$i] = "-1";
						}
						else
						{
							$FAIL = true;
							$messErr = mysql_error();
						}
					}
					else
					{
						$idOfAchievementCreat[$i] = $_SESSION['AchIdTwo'][$i];
					}
				}
				else
				{
					$FAIL = true;
					$messErr = mysql_error();
				}
			}
			else
			{
				$idOfAchievementCreat[$i] = $_SESSION['AchIdTwo'][$i];
			}
			
		}
		else
		{
			$idOfAchievementCreat[$i] = -1;
		}
		$j--;
	}
}
if(!$FAIL)
{
	$sql = "UPDATE `achmw_achievements` SET name='".$_SESSION['AchTitle']."',description='".$_SESSION['AchDesc']."',img='".$_SESSION['Achimage']."',amount=".$_SESSION['Achamount'].",cat_id=".$_SESSION['AchCat'].",ResetBlocksOnDeath=".$_SESSION['Achonelife'].",OnComplete=".$_SESSION['Achtaunt'].",ResetTime=".$_SESSION['Achtime'];
	$sql = $sql .",blocka_typ='".substr($table_insert[5],strlen("achmw_blocks_"))."',blocka_id=".$idOfAchievementCreat[1].",blocka_max=";
	if ($args[5][0] != null)
		$sql = $sql.$args[5][0];
	else
		$sql = $sql."1";
	
	$sql = $sql .",blockb_typ='".substr($table_insert[4],strlen("achmw_blocks_"))."',blockb_id=".$idOfAchievementCreat[2].",blockb_max=";
	if ($args[4][0] != null)
		$sql = $sql.$args[4][0];
	else
		$sql = $sql."1";
	
	$sql = $sql .",blockc_typ='".substr($table_insert[3],strlen("achmw_blocks_"))."',blockc_id=".$idOfAchievementCreat[3].",blockc_max=";
	if ($args[4][0] != null)
		$sql = $sql.$args[4][0];
	else
		$sql = $sql."1";
		
	$sql = $sql .",blockd_typ='".substr($table_insert[2],strlen("achmw_blocks_"))."',blockd_id=".$idOfAchievementCreat[4].",blockd_max=";
	if ($args[3][0] != null)
		$sql = $sql.$args[3][0];
	else
		$sql = $sql."1";
	
	$sql = $sql .",blocke_typ='".substr($table_insert[1],strlen("achmw_blocks_"))."',blocke_id=".$idOfAchievementCreat[5].",blocke_max=";
	if ($args[2][0] != null)
		$sql = $sql.$args[2][0];
	else
		$sql = $sql."1";
	
	$sql = $sql." WHERE id = ".$_SESSION['AchId'].";";
	//echo $sql;
	$result = mysql_query($sql) or $not2 = true;
	if (!$result) 
	{
		$FAIL = true;
		$messErr = mysql_error();
	}
	$idOfAchievementCreat = $_SESSION['AchId'];
}
$timeleft = array ('command' => 'timeleft',
                   'outputs' => array ('This is the last round!!' => 'Last round',
                                       'No timelimit for map' => 'No time limit',
                                       'Time remaining for map:\s+([0-9]+:[0-9]+)' => 'Timeleft: #1#'),
                   'default' => ''
);

if (!$FAIL) 
{
	require ('../function/class_PQ.php');
	$servers = $CONFIG['server'];
	$timeout = $CONFIG['timeout'];
	$retries = $CONFIG['retries'];
	foreach ($servers as $server) 
	{
		$pq = PQ::create(array(
			'ip' 		=> $server['ip'],
			'port'		=> $server['port'],
			'querytype'	=> 'halflife',
			'timeout' 	=> $timeout, 
			'retries' 	=> $retries,
		));
        
		$pqinfo = @$pq->query(array('info'));
		
		$placeholders[$server['ip'].":".$server['port']] ['#name#'] = $server ['name'];
		echo $server ['name'];
		$placeholders[$server['ip'].":".$server['port']] ['#ip#'] = $server ['ip'];
		$placeholders[$server['ip'].":".$server['port']] ['#port#'] = $server ['port'];
		$placeholders[$server['ip'].":".$server['port']] ['#map#'] = (isset ($pqinfo ['map'])) ? $pqinfo ['map'] : ''; 
		$placeholders[$server['ip'].":".$server['port']] ['#players#'] = (isset ($pqinfo ['totalplayers'])) ? $pqinfo ['totalplayers'] : '';
		$placeholders[$server['ip'].":".$server['port']] ['#maxplayers#'] = (isset ($pqinfo ['maxplayers'])) ? $pqinfo ['maxplayers'] : '';
		
		if (isset ($timeleft) && is_array ($timeleft)) {
			
			$res = @$pq -> rcon ($timeleft ['command'], $server ['pass']);
			$match = false;
			foreach ($timeleft ['outputs'] as $regexp => $out) 
			{
				if (preg_match ('@' . $regexp . '@' , $res, $arr)) 
				{
					$trtable = array ();
					for ($i = 0, $j = count ($arr); $i < $j; $i++) {
						$trtable ['#' . $i . '#'] = $arr [$i];    
					}
					$timeleft = strtr ($out, $trtable);
					$placeholders[$server['ip'].":".$server['port']] ['#timeleft#'] = $timeleft;
					print_r($timeleft);
					$match = true;
					break; 
				}    
			}
			if (!$match) {  
				$placeholders[$server['ip'].":".$server['port']] ['#timeleft#'] = "Last round";
			}        
		}
	}
}
?>

<div id="confpart">
<div class="editpart">
<br/><br/>
<div class="divtypeach">
<div class="divblocktype"><center><h3><?php 
if($FAIL) 
	echo $ERRORONDATABASE[$Lang].$messErr."<br/>".$sql; 
else 
	if($GET_VAR_PAGE=="creation")
		echo $SUCCESSOFCREATION[$Lang];
	else
		echo $SUCCESSOFMODIF[$Lang];
?>
</h3></center></div>
<?php
if(!$FAIL)
{
?>
<center><img src="../achimg.php?a=<?php echo $idOfAchievementCreat; ?>" alt="NEW"></center>
<br/>
<span class="divblocktype"><h3><?php echo $TIMEREFRESH[$Lang]; ?></h3></span>
<?php
$i = 0;
foreach ($servers as $server) 
{
	$pq = PQ::create(array(
			'ip' 		=> $server['ip'],
			'port'		=> $server['port'],
			'querytype'	=> 'halflife',
			'timeout' 	=> $timeout, 
			'retries' 	=> $retries,
		));
	$pqinfo = @$pq->query(array('info'));
	?>
	<div class="divServerGameOn">
		<table style="text-align: left; width: 90%;" border="0" cellpadding="0" cellspacing="10">
			<tbody>
				<tr>
					<td style="width: 60%;">
					<span class="GameServerTitle"><?php echo $server['name']; ?> (<?php echo $placeholders[$server['ip'].":".$server['port']] ['#players#']; ?>/<?php echo $placeholders[$server['ip'].":".$server['port']] ['#maxplayers#']; ?>)</span><br/>
					<span class="GameServerMap">Map : <?php echo $placeholders[$server['ip'].":".$server['port']] ['#map#']; ?><br/> (<?php echo $TIMELEFT[$Lang].$placeholders[$server['ip'].":".$server['port']]['#timeleft#'];?>)</span><br/>
					<span class="GameServerIp"><?php echo $server['ip'].":".$server['port']; ?></span><br/>
					</td>
					<td style="width: 40%;">
					<div class="divServerCountDown"><?php						
							echo $LASTROUND[$Lang];
							echo "<br/>";
						?></div>
					</td>
				</tr>
			</tbody>
		</table>
	</div>
	<br/>
	<?php
$i++;
} ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="formSaisie">
<center><input value="<?php echo $RETURNONHOME[$Lang]; ?>" type="submit" style="font-size:125%; width:500px; height:35px; font-family: TF2Font, Verdana;"></center>
</form>
<br/><br/>
<?php
}
if(!$FAIL)
{
	unset($_SESSION['AchTitle']); 
	unset($_SESSION['AchDesc']); 
	unset($_SESSION['AchCat']); 
	unset($_SESSION['Achimage']); 
	unset($_SESSION['Achamount']); 
	unset($_SESSION['Achonelife']); 
	unset($_SESSION['Achtaunt']);
	unset($_SESSION['Achtime']);
		
	unset($_SESSION['AchTypeofAchievement']);	
	//Kill
	unset($_SESSION['AchWhoWinKill']); 		 		
	unset($_SESSION['AchKillVictimSteamIDText']); 	
	unset($_SESSION['AchKillAttackSteamIDText']); 	
	unset($_SESSION['AchKillAssistSteamIDText']); 	
	unset($_SESSION['AchKillWeapon']); 				
	unset($_SESSION['AchKillMap']); 					
	unset($_SESSION['AchKillVictimclass']);			
	unset($_SESSION['AchKillAttackclass']); 			
	unset($_SESSION['AchKillAssistclass']); 			
	unset($_SESSION['AchKillCritz']); 
	unset($_SESSION['AchKillDomination']);
	unset($_SESSION['AchKillRevenge']); 		
	unset($_SESSION['AchKillHeadShot']); 	
	unset($_SESSION['AchKillBackstab']); 	
	unset($_SESSION['AchKillDeadRinger']); 			
	unset($_SESSION['AchKillAmount']);					
	//Sniper
	unset($_SESSION['AchSniperJarateRazor']); 		
	unset($_SESSION['AchSniperAttackVictim']); 		
	unset($_SESSION['AchSniperVictimSteamID']); 	
	unset($_SESSION['AchSniperAmount']); 			
	//Engineer
	unset($_SESSION['AchEngineerChoose']); 			
	unset($_SESSION['AchEngineerObjectName']);		
	unset($_SESSION['AchEngineerDestroy']); 			
	unset($_SESSION['AchEngineerDestroyBuild']);	
	unset($_SESSION['AchEngineerOwnerSteamID']);
	unset($_SESSION['AchEngineerUserSteamID']);
	unset($_SESSION['AchEngineerUserClass']);
	unset($_SESSION['AchEngineerSelfUse']);
	unset($_SESSION['AchEngineerAmount']); 			
	//Fun
	unset($_SESSION['AchFunRadio']);		 			
	unset($_SESSION['AchFunAmount']); 
	unset($_SESSION['AchFunRadioExact']);		 	
	unset($_SESSION['AchFunSayValue']);		 		
	unset($_SESSION['AchFunAmount']); 
	//Heavy
	unset($_SESSION['AchHeavySandwitch']);		 	
	unset($_SESSION['AchHeavySandwitchSteal']); 		
	unset($_SESSION['AchHeavyAmount']); 				
	//Hurt
	unset($_SESSION['AchWhoWinHurt']); 		 		
	unset($_SESSION['AchHurtAttackerSteamIDText']); 	
	unset($_SESSION['AchHurtVictimSteamIDText']); 	
	unset($_SESSION['AchHurtCritz']);			 	
	unset($_SESSION['AchHurtDmg_min']); 				
	unset($_SESSION['AchHurtDmg_max']); 	
	unset($_SESSION['AchHurtSelfdmg']);
	unset($_SESSION['AchHurtMap']);					
	unset($_SESSION['AchHurtAttacker_class']); 		
	unset($_SESSION['AchHurtVictim_class']); 		
	unset($_SESSION['AchHurtAmount']);				
	//Hurt
	unset($_SESSION['AchWhoWinTeamMap']); 		 	
	unset($_SESSION['AchMapCaperSteamIDText']); 		
	unset($_SESSION['AchMapName']); 					
	unset($_SESSION['AchMapNumberOfPoint']);			
	unset($_SESSION['AchMapMinCaper']); 				
	unset($_SESSION['AchMapMaxCaper'] );				
	unset($_SESSION['AchMapCapturerClass']);			
	unset($_SESSION['AchMapAmount']); 				
	//MEDIC
	unset($_SESSION['AchMedicUber']);	 		 	
	unset($_SESSION['AchMedicAmount']);	
	unset($_SESSION['AchMedicWho']); 
	unset($_SESSION['AchMedicAssistSteamID']); 	
	//Scout
	unset($_SESSION['AchScoutAchievements']);	 	
	unset($_SESSION['AchScoutAchievementsStunt']);	
	unset($_SESSION['AchScoutAmount']);		
	unset($_SESSION['AchScoutVictimSteamID']); 		
	//Condition
	unset($_SESSION['AchB2Conditon']);			
	unset($_SESSION['AchConditonWho']);		
	unset($_SESSION['AchB2Conditon']);
	unset($_SESSION['AchB2ConditonChapeau']);	
	unset($_SESSION['AchB2ConditonHealth1']);
	unset($_SESSION['AchB2ConditonHealth2']);		
	unset($_SESSION['partCreation']);
	
	unset($_SESSION['AchDemomanAmount']);
	unset($_SESSION['AchSoldierAction']);
	unset($_SESSION['AchSoldierAmount']);
	unset($_SESSION['AchSpyWho']);
	unset($_SESSION['AchSpyWho']);
	unset($_SESSION['AchSpyObjectName']);
	unset($_SESSION['AchSpyVictimSteamID']);
	unset($_SESSION['AchSpyAmount']);
}

?>

</div>
<br/><br/>
</div>
</div>